package com.example.restful.controller;

import com.example.restful.common.CommonResponse;
import com.example.restful.common.LinkDTO;
import com.example.restful.common.LinkInfo;
import com.example.restful.domain.Personnel;
import com.example.restful.service.PersonnelService;
import com.example.restful.utils.UrlUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.Link;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController()
@RequestMapping("personnels")
@Tag(name = "实验室人员接口")
public class PersonnelController {

    @Autowired
    PersonnelService personnelService;

    @GetMapping()
    @Operation(summary = "获取所有实验室人员的信息")
    public CommonResponse<Object> getAll() {
        List<Personnel> list = personnelService.selectAll();
        List<LinkDTO<Personnel>> res = new ArrayList<>();
        for (Personnel personnel : list) {
            Map<String,String> urls = new HashMap<>();
            urls.put(LinkInfo.personnelInfo,UrlUtil.getUrlWithArg(PersonnelController.class, "getById", personnel.getId()));
            urls.put(LinkInfo.personnelBorrow,UrlUtil.getUrlWithArg(BorrowController.class, "getByPersonnelId", personnel.getId()));
            LinkDTO<Personnel> linkDTO = new LinkDTO<>(personnel, urls);
            res.add(linkDTO);
        }
        return CommonResponse.createForSuccess("查询实验人员成功", res);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取某个指定的实验室人员的信息")
    public CommonResponse<Object> getById(@PathVariable int id) {
        Personnel personnel = personnelService.selectById(id);
        Map<String,String> urls = new HashMap<>();
        urls.put(LinkInfo.allPersonnel,UrlUtil.getUrl(PersonnelController.class, "getAll"));
        urls.put(LinkInfo.personnelBorrow,UrlUtil.getUrlWithArg(BorrowController.class, "getByPersonnelId", personnel.getId()));
        LinkDTO<Personnel> res = new LinkDTO<>(personnel, urls);
        return CommonResponse.createForSuccess("查询实验人员成功", res);
    }

    @PostMapping()
    @Operation(summary = "增加一个实验室人员")
    public CommonResponse<Object> addPersonnel(@RequestBody Personnel personnel) {

        if (personnelService.addItem(personnel)) {
            return CommonResponse.createForSuccess("添加实验人员成功", personnel);
        }
        return CommonResponse.createForError("添加实验人员失败");
    }

    @PutMapping("/{id}")
    @Operation(summary = "修改某个指定实验室人员的信息")
    public CommonResponse<Object> update(@PathVariable int id, @RequestBody Personnel personnel) {
        if (personnelService.updateById(id, personnel)) {
            Personnel newPersonnel = personnelService.selectById(id);
            return CommonResponse.createForSuccess("修改实验人员成功", newPersonnel);
        }
        return CommonResponse.createForError("修改实验人员失败");
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除某个指定的实验室人员")
    public CommonResponse<Object> delete(@PathVariable int id) {
        if (personnelService.deleteById(id)) {
            return CommonResponse.createForSuccess("删除实验人员成功");
        }
        return CommonResponse.createForError("删除实验人员失败");
    }

}
